package com.biaobiaoqi.scoreanalysis.serviceImpl;

import java.util.List;

import com.biaobiaoqi.scoreanalysis.exception.AuthenticateFailedException;
import com.biaobiaoqi.scoreanalysis.exception.DuplicateLoginNameException;
import com.biaobiaoqi.scoreanalysis.exception.NoSuchEntityFoundException;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.Administrator;
import com.biaobiaoqi.scoreanalysis.security.PasswordEncryption;
import com.biaobiaoqi.scoreanalysis.service.IAdministratorService;

public class AdministratorServiceImpl extends DaoServiceImpl<Administrator> implements IAdministratorService {
	
	@Override
	public void addAdministrator(String loginName, String userName,
			String password, String email)  throws DuplicateLoginNameException{
		if (!isLoginNameAvailable(loginName)) {
			throw new DuplicateLoginNameException();
		}
		Administrator administrator = new Administrator();
		administrator.setLoginName(loginName); 
		administrator.setName(userName);
		administrator.setPassword(
				PasswordEncryption.getInstance().encryptPassword(password));
		administrator.setEmail(email);
		getDao().create(administrator);
	}

	@Override
	public void deleteAdministrator(int id) throws NoSuchEntityFoundException {
		if (getAdministrator(id)==null) {
			throw new NoSuchEntityFoundException();
		}
		getDao().delete(getAdministrator(id));
	}

	@Override
	public void deleteAdministrator(String loginName) throws NoSuchEntityFoundException {
		if (isLoginNameAvailable(loginName)) {
			throw new NoSuchEntityFoundException();
		}
		getDao().delete(getAdministrator(loginName));
	}

	@Override
	public Administrator authenticateAdministrator(String loginName, String password) throws AuthenticateFailedException   {
		Administrator administrator = getAdministrator(loginName);
		if (administrator==null) {
			throw new AuthenticateFailedException();
		}
		if (!administrator.getPassword().equals(
				PasswordEncryption.getInstance().encryptPassword(password))) {
			throw new AuthenticateFailedException();  
		} 
		return administrator;
	} 
    
	@Override
	public List<Administrator> listAllAdministrator() {
		return getDao().queryList("select a from Administrator a");
	}
	
	@Override
	public List<Administrator> listAllAdministrator(int from, int number) {
		return getDao().queryList("select a from Administrator a", from, number);
	}

	@Override
	public Administrator getAdministrator(String loginName) {
		return getDao().queryUnique("select a from Administrator a where a.loginName=?", loginName);
	}

	@Override
	public Administrator getAdministrator(int id) {
		return getDao().findById(Administrator.class, id);
	}

	@Override
	public void modifyAdministratorName(int id, String userName) throws NoSuchEntityFoundException {
		Administrator administrator = getAdministrator(id);
		if (administrator==null) {
			throw new NoSuchEntityFoundException();
		}
		administrator.setName(userName);
 		getDao().save(administrator);
	}

	@Override
	public boolean isLoginNameAvailable(String loginName) {
		Administrator administrator = getAdministrator(loginName); 
		if (administrator==null) {
			return true;
		}
		return false;
	}

	@Override
	public void modifyAdministratorPassword(int id,String originalPassword, String newPassword) throws AuthenticateFailedException, NoSuchEntityFoundException {
		Administrator administrator = getAdministrator(id);
		if (administrator==null) {
			throw new NoSuchEntityFoundException();
		}
		if (!administrator.getPassword().equals(PasswordEncryption.getInstance().encryptPassword(originalPassword))) {
			throw new AuthenticateFailedException();
		} else {
			administrator.setPassword(PasswordEncryption.getInstance().encryptPassword(newPassword));
			getDao().save(administrator);
		}
		
	}
}
